﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace cnFederal
{
    public class Fvideos
    {
        private static CnfiDataDataContext lnq;
        private static List<Fvideo> _videoList;
        public List<Fvideo> VideoList
        {
            get
            {
                return _videoList;
            }
            set
            {
                _videoList = value;
            }
        }

        public static void Load(bool display, bool all = false)
        {
            lnq = new CnfiDataDataContext();
            _videoList = new List<Fvideo>();
            if (_videoList != null)
            {
                List<Fvideo> liste = new List<Fvideo>();
                var b = from c in lnq.FaVideos
                        where c.IsVisible == display
                        orderby c.Id
                        select c;
                if(all)
                 b = from c in lnq.FaVideos
                        orderby c.Id
                        select c;

                foreach (var s in b)
                {
                    Fvideo video = new Fvideo();
                    video.Id = (int)s.Id;
                    video.Title = (string)s.Title;
                    video.FileType = (string)s.FileType;
                    video.FilePath = (string)s.FilePath;
                    if (s.DateRegistered != null)
                        video.DateRegistered = (DateTime)s.DateRegistered;
                    if (s.DateEndPublish != null)
                        video.DateEndPublish = (DateTime)s.DateEndPublish;
                    if (s.DateStartPublish != null)
                        video.DateStartPublish = (DateTime)s.DateStartPublish;
                    if (s.UserId != null)
                        video.UserId = (int)s.UserId;
                    if (s.IsVisible != null)
                        video.IsVisible = (bool)s.IsVisible;
                    liste.Add(video);
                }

                _videoList = liste;
            }
        }


        public void RefreshPostlist()
        {
            _videoList.Clear();
            _videoList = GetAllFvideos();
        }

        public static List<Fvideo> GetAllFvideos()
        {

            return Enumerable.Reverse(_videoList).ToList(); ;
        }

        public static List<Fvideo> GetTopVideos(int number)
        {
            List<Fvideo> liste = GetAllVideosFromDateToDate();
            if (liste == null) return null;
             var b = liste.Take(number);
            return b.ToList();
        }

        public static List<Fvideo> GetAllVideosFromDateToDate()
        {
            List<Fvideo> liste = new List<Fvideo>();
            if (_videoList != null)
            {
                foreach (Fvideo post in _videoList)
                {
                    if (post.DateStartPublish <= DateTime.Now &&
                       post.DateEndPublish >= DateTime.Now)
                        liste.Add(post);
                }

            }

            return Enumerable.Reverse(liste).ToList();
        }
        public static Fvideo GetVideoById(int id)
        {
            if (_videoList == null) return null;
            Fvideo video = null;
            var b = from c in _videoList
                    where c.Id == id
                    select c;
            foreach (var s in b)
            {
                video = new Fvideo();
                video.Id = (int)s.Id;
                video.Title = (string)s.Title;
                video.FileType = (string)s.FileType;
                video.FilePath = (string)s.FilePath;
                video.DateRegistered = (DateTime)s.DateRegistered;
                video.DateStartPublish = (DateTime)s.DateStartPublish;
                video.DateEndPublish = (DateTime)s.DateEndPublish;

                video.UserId = (int)s.UserId;
                video.IsVisible = (bool)s.IsVisible;

            }

            return video;
        }


        public static bool CreateNew(Fvideo f)
        {
            bool created = false;
            try
            {
                FaVideo fa = new FaVideo();
                fa.Title = f.Title;
                fa.FileType = f.FileType;
                fa.FilePath = f.FilePath;
                fa.UserId = (int)f.UserId;
                fa.IsVisible = (bool)f.IsVisible;
                fa.DateEndPublish = f.DateEndPublish;
                fa.DateRegistered = f.DateRegistered;
                fa.DateStartPublish = f.DateStartPublish;              
                lnq.FaVideos.InsertOnSubmit(fa);
                lnq.SubmitChanges();
                lnq.Connection.Close();
                created = true;
            }
            catch (Exception ex)
            {
                created = false;
            }
            return created;
        }

        public static bool DeleteItem(int id)
        {
            bool deleted = false;
            var b = from f in lnq.FaVideos
                    where f.Id == id
                    select f;
            if (b != null && b.Count() > 0)
            {
                FaVideo n = b.First();
                try
                {
                    lnq.FaVideos.DeleteOnSubmit(n);
                    lnq.SubmitChanges();
                    deleted = true;
                }
                catch (Exception ex)
                {
                    deleted = false;
                }
            }
            return deleted;
        }

        public static void Save(Fvideo fv)
        {
            var b = from f in lnq.FaVideos
                    where f.Id == fv.Id
                    select f;
            foreach (var n in b)
            {
                
                n.DateEndPublish = fv.DateEndPublish;
                n.DateStartPublish = fv.DateStartPublish;
                n.FilePath = fv.FilePath;
                n.FileType = fv.FileType;
                n.IsVisible = fv.IsVisible;
                n.Title = fv.Title;
                n.UserId = fv.UserId;
               
            }
            lnq.SubmitChanges();
        }




    }

    public class Fvideo
    {
        public int Id
        {
            get;
            set;
        }

        public string Title
        {
            get;
            set;
        }
        public string FileType
        {
            get;
            set;
        }
        public string FilePath
        {
            get;
            set;
        }

        public DateTime DateRegistered
        {
            get;
            set;
        }
        public DateTime DateStartPublish
        {
            get;
            set;
        }

        public DateTime DateEndPublish
        {
            get;
            set;
        }

        public int UserId
        {
            get;
            set;
        }

        public bool IsVisible
        {
            get;
            set;
        }
    }
}